<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderId">
        <el-select v-model="queryParams.orderId" placeholder="请选择订单编号" clearable size="small">
          <el-option
            v-for="ID in order_ID"
            :key="ID.orderId"
            :value="ID.orderId"
            :label="ID.orderId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择用户名称" clearable size="small">
          <el-option
            v-for="ID in user_ID"
            :key="ID.userId"
            :value="ID.userId"
            :label="ID.userName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工作时间" prop="workTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.workTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择工作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工作地点" prop="workPlace">
          <el-select v-model="queryParams.workPlace" placeholder="请输入工作地点" clearable filterable allow-create size="small">
            <el-option
              v-for="ID in greenhouse_ID"
              :key="ID.greenhouseName"
              :value="ID.greenhouseName"
              :label="ID.greenhouseName"
            ></el-option>
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['process:grafting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['process:grafting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['process:grafting:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['process:grafting:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="graftingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="订单名称" align="center" prop="orderName" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="工作时间" align="center" prop="workTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作地点" align="center" prop="workPlace" />
      <el-table-column label="工作量" align="center" prop="workLoad" />
      <el-table-column label="工作量单位" align="center" prop="workUnit" />
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="审批" align="center" prop="approval" >
      <template slot-scope="scope">
        <span>
          {{approvalStatus[scope.row.approval]}}
        </span>
        <div style="display:flex">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-check"
          @click="handlethrough(scope.row)"
          v-hasPermi="['process:grafting:edit']"
        >通过</el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-close"
          @click="handleNotThrough(scope.row)"
          v-hasPermi="['process:grafting:edit']"
        >不通过</el-button>
        </div>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['process:grafting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['process:grafting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改嫁接管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="订单编号" prop="orderId">
          <el-select v-model="form.orderId" placeholder="请选择订单编号" clearable size="small">
            <el-option
              v-for="ID in order_ID"
              :key="ID.orderId"
              :value="ID.orderId"
              :label="ID.orderId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名称" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择用户名称" clearable size="small">
            <el-option
              v-for="Id in user_ID"
              :key="Id.userId"
              :value="Id.userId"
              :label="Id.userName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作时间" prop="workTime">
          <el-date-picker clearable size="small"
            v-model="form.workTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择工作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作地点" prop="workPlace">
          <el-select v-model="form.workPlace" placeholder="请输入工作地点" clearable filterable allow-create size="small">
            <el-option
              v-for="ID in greenhouse_ID"
              :key="ID.greenhouseName"
              :value="ID.greenhouseName"
              :label="ID.greenhouseName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作量" prop="workLoad">
          <el-input v-model.number="form.workLoad" placeholder="请输入工作量" />
        </el-form-item>
        <el-form-item label="工作量单位" prop="workUnit">
          <el-input v-model="form.workUnit" placeholder="请输入工作量单位" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrafting, getGrafting, delGrafting, addGrafting, updateGrafting, exportGrafting,listUser,listOrder,listgreenhouse } from "@/api/process/grafting";

export default {
  name: "Grafting",
  components: {
  },
  data() {
    return {
      //审批状态
      approvalStatus:{
        0:'未审批',
        1:'审批通过',
        2:'未通过'
      },
      user_ID:[],
      order_ID:[],
      greenhouse_ID:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 嫁接管理表格数据
      graftingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderId: null,
        workTime: null,
        workPlace: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户编号不能为空", trigger: "change" }
        ],
        orderId: [
          { required: true, message: "订单编号不能为空", trigger: "change" }
        ],
        workTime: [
          { required: true, message: "工作时间不能为空", trigger: "blur" }
        ],
        workPlace: [
          { required: true, message: "工作地点不能为空", trigger: "change" }
        ],
        workLoad: [
          { required: true, message: "工作量不能为空", trigger: "blur" },
          { type : "number",message:"请输入数字",trigger:"blur"}
        ],
        workUnit: [
          { required: true, message: "工作量单位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getgreenhouseID();
    this.getuserID();
    this.getorderID();
  },
  methods: {
    /**通过按钮操作*/
    handlethrough(row){
      this.reset();
      this.form.note="tonguo"
      const workGraftingId = row.workGraftingId||this.ids
      let data={}
      data.workGraftingId=workGraftingId;
      data.approval=1;
      this.$confirm('是否确认通过序号为"'+workGraftingId+'"的数据项？',"提示",{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(function(){
        return updateGrafting(data).then(response=>{
          console.log(response)
        })
      }).then(()=>{
        this.getList();
      }).catch(()=>{});
    },
    /**不通过按钮操作*/
    handleNotThrough(row){
      const workGraftingId = row.workGraftingId||this.ids;
      let data={}
      data.workGraftingId=workGraftingId;
      data.approval=2;
      this.$confirm('是否确认不通过序号为"'+workGraftingId+'"的数据项？',"警告",{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(function(){
        return updateGrafting(data).then(response=>{
          console.log(response)
        })
      }).then(()=>{
        this.getList();
      }).catch(()=>{});
    },
    /**获取订单名称*/
    formdata_graftingOrderList(data){
      let grafting_type_map = new Map();
      this.order_ID.forEach(type =>{
        grafting_type_map.set(type.orderId,type.orderName)
      })
      data.forEach(type => {
        let order_name = grafting_type_map.get(type.orderId)
        console.log(order_name)
        type.orderName = order_name
        console.log(type.orderName)
      });
    },
    /**获取用户名称*/
    formdata_graftingUserList(data){
      let grafting_type_map = new Map();
      this.user_ID.forEach(type =>{
        grafting_type_map.set(type.userId,type.userName)
      })
      data.forEach(type => {
        let user_name = grafting_type_map.get(type.userId)
        console.log(user_name)
        type.userName = user_name
        console.log(type.userName)
      });
    },
    /** 查询嫁接管理列表 */
    getList() {
      this.loading = true;
      listGrafting(this.queryParams).then(response => {
        this.graftingList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.formdata_graftingOrderList(this.graftingList);
        this.formdata_graftingUserList(this.graftingList)
      });
    },
    getuserID(){
      listUser().then(response =>{
        //console.log(response)
        this.user_ID = response.rows;
      })
    },
    getorderID(){
      listOrder().then(response =>{
        console.log(response)
        this.order_ID = response.rows;
      })
    },
    getgreenhouseID(){
      listgreenhouse().then(response =>{
        //console.log(response)
        this.greenhouse_ID = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workGraftingId: null,
        userId: null,
        orderId: null,
        workTime: null,
        workPlace: null,
        workLoad: null,
        workUnit: null,
        note: null,
        approval: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workGraftingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加嫁接管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workGraftingId = row.workGraftingId || this.ids
      getGrafting(workGraftingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改嫁接管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workGraftingId != null) {
            updateGrafting(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrafting(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workGraftingIds = row.workGraftingId || this.ids;
      this.$confirm('是否确认删除嫁接管理编号为"' + workGraftingIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGrafting(workGraftingIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有嫁接管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportGrafting(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
